<?php
namespace app\admin\model\agent;
use think\Model;
use think\Db;
class Agent extends Model{
    protected $name = 'yonghui_customer';
    /**
     * 获取客户列表
     */
    public function getList($where,$companyId){
         //获取列表数据
        if(!empty($where['search_time'])&&empty($where['search'])){
            $a=explode('至',$where['search_time']);
            $w['createTime']=['between',[strtotime($a['0']),strtotime($a['1'])]];
        }elseif (!empty($where['search_time'])&&!empty($where['search'])){
            $a=explode('至',$where['search_time']);
            $w['createTime']=['between',[strtotime($a['0']),strtotime($a['1'])]];
            $w['name|phone']=['like','%'.$where['search'].'%'];
        }elseif (empty($where['search_time'])&&!empty($where['search'])){
            $w['name|phone']=['like','%'.$where['search'].'%'];
        }else{
            $w=[];
        }
        $iid=[];
        $iid1=[];
        if(!empty($where['lururen'])){
            $ids1=Db::name('yonghui_user')->where(['name'=>$where['lururen']])->select()->toArray();
            if($ids1){
                foreach($ids1 as $k=>$v){
                    $iid1[]=$v['id'];
                }
            }
            //
        }
        if(isset($where['projectId'])&&$where['projectId']!=0){
            //根据板块id获取录入人Id
            $ids=Db::name('yonghui_user')->where(['projectId'=>$where['projectId']])->select()->toArray();
            if($ids){
                foreach($ids as $k=>$v){
                    $iid[]=$v['id'];
                }
            }
        }
        if($iid&&$iid1){
            $array_merge = array_intersect($iid1,$iid);
            $w['inputId']=['in',$array_merge];
        }
        if(!$iid&&$iid1){
            $w['inputId']=['in',$iid1];
        }
        if($iid&&!$iid1){
            $w['inputId']=['in',$iid];
        }

        if(isset($where['buildId'])&&$where['buildId']!=0){
            $w['propertiesId']=['=',$where['buildId']];
        }
        if(isset($where['status'])&&$where['status']!=0){
            $w['followStatus']=['=',$where['status']];
        }
        if($companyId[0]!=0){
            return $this->where($w)
                ->where('sourceId', 'in',$companyId)
                ->order(['createTime' => 'desc'])
                ->paginate(15, false, [
                    'query' => request()->request()
                ]);
        }else{
            return $this->where($w)
                ->order(['createTime' => 'desc'])
                ->paginate(15, false, [
                    'query' => request()->request()
                ]);
        }
    }
}